home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / libf.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  4KB  |  173 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. /* libf - auxiliary procedures for reading in IFILE format files.
  11.  * This is subset of procedures needed to read files generated in
  12.  * library format without the need all the library primitives.
  13.  */
  14.  
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "config.h"
  19. #include "ifile.h"
  20. #include "miscp.h"
  21. #include "libfp.h"
  22. #ifndef TRUE
  23. #define TRUE 1
  24. #endif
  25.  
  26. int getint(IFILE *ifile, char *desc)                            /*;getint*/
  27. {
  28.     /* read int  from input file */
  29.     int si;
  30.     int n = 0;
  31.     int    nr;
  32.  
  33.     nr = fread((char *) &si, sizeof(int), 1, ifile->fh_file);
  34.     if (nr != 1) {
  35.         chaos("libr.c: read_ais - unable to read int value");
  36.     }
  37.     n = si;
  38.     return n;
  39. }
  40.  
  41. int getnum(IFILE *ifile, char *desc)                                /*;getnum*/
  42. {
  43.     /* read integer (only 2 bytes) from input file */
  44.  
  45.     short si;
  46.     int n = 0;
  47.  
  48.     fread((char *) &si, sizeof(short), 1, ifile->fh_file);
  49.     n = si;
  50.     return n;
  51. }
  52.  
  53. int getchr(IFILE *ifile, char *desc)                                /*;getchr*/
  54. {
  55.     /* This is variant of getnum used when reading character values */
  56.     /* read integer (only 2 bytes) from input file */
  57.  
  58.     short si;
  59.     int n = 0;
  60.  
  61.     fread((char *) &si, sizeof(short), 1, ifile->fh_file);
  62.     n = si;
  63.     return n;
  64. }
  65.  
  66. long getlong(IFILE *ifile, char *desc)                            /*;getlong*/
  67. {
  68.     /* read long  from input file */
  69.  
  70.     long si;
  71.     long n = 0;
  72.     int    nr;
  73.  
  74.     nr = fread((char *) &si, sizeof(long), 1, ifile->fh_file);
  75.     if (nr != 1) {
  76.         chaos("libr.c: read_ais - unable to read long value");
  77.     }
  78.     n = si;
  79.     return n;
  80. }
  81.  
  82. char *getstr(IFILE *ifile, char *desc)                            /*;getstr*/
  83. {
  84.     char    *s, *p;
  85.     int        n, i;
  86.  
  87.     n = getnum(ifile, "");
  88.     if (n == 0) return (char *)0;
  89.     s = (char *) smalloc((unsigned) n);
  90.     p = s;
  91.     for (i = 1; i < n; i++) {
  92.         *p++ = getc(ifile->fh_file);
  93.     }
  94.     *p = '\0'; /* set end of string*/
  95.  
  96.     return s;
  97. }
  98.  
  99. long read_init(IFILE *ifile)                                /*;read_init*/
  100. {
  101.     /* initialize read, position at start of first record, return
  102.      * offset of next record. return 0 if no first first record.
  103.      * read first word in file that may have offset to slot info.
  104.      */
  105.  
  106.     long  pos, start;
  107.     int   nr;
  108.  
  109.     nr = fread((char *) &pos, sizeof(long), 1, ifile->fh_file);
  110.     if (nr != 1) {
  111.         chaos("read_init read failed ");
  112.         return 0;
  113.     }
  114.     return pos;
  115. }
  116.  
  117. long read_next(IFILE *ifile, long p)                        /*;read_next*/
  118. {
  119.     int     nr;
  120.     long  pos;
  121.  
  122.     ifseek(ifile, "next-unit", p, 0);
  123.     if (p == ifile->fh_units_end) {
  124.         return 0; /* if at end */
  125.     }
  126.     nr = fread((char *) &pos, sizeof(long), 1, ifile->fh_file);
  127.     if (nr != 1) {
  128.         chaos("read_next read failure");
  129.         return 0;
  130.     }
  131.     return pos;
  132. }
  133.  
  134. void putnum(IFILE *ofile, char *desc, int n)                        /*;putnum*/
  135. {
  136.     /* write integer (as a short) to output file */
  137.  
  138.     short s;
  139.     s = n;
  140.  
  141.     fwrite((char *) &s, sizeof(short), 1, ofile->fh_file);
  142. }
  143.  
  144. void putpos(IFILE *ofile, char *desc, int n)                    /*;putpos*/
  145. {
  146.     /* like putnum, but verifies that argument positive */
  147.     /* write integer (as a short) to output file */
  148.  
  149.     if (n < 0) chaos("putpos: negative argument");
  150.     putnum(ofile, desc, n);
  151. }
  152.  
  153. void putstr(IFILE *ofile, char *desc, char *s)                    /*;putstr*/
  154. {
  155.     if (s == (char *)0) {
  156.         putnum(ofile, "", 0);
  157.     }
  158.     else {
  159.         putnum(ofile, "", strlen(s)+1);
  160.         fputs(s, ofile->fh_file);
  161.     }
  162. }
  163.  
  164. void putchr(IFILE *ofile, char *desc, int n)                    /*;putchr*/
  165. {
  166.     /* variant of putnum used when writing character value */
  167.     /* write integer (as a short) to output file */
  168.  
  169.     short s = n;
  170.  
  171.     fwrite((char *) &s, sizeof(short), 1, ofile->fh_file);
  172. }
  173.